/*	kd:
	
	This does screen projections for open-gl, so you can do things like drawing
	text over actors or whatever.
	
	Note that the base mod doesn't even use this!
	
	Look for gc_addon.pk3, sandman visor.txt and there's a pretty
	straightforward example on how to use this.
	
	*/

class Gc_GlScreen {
	
	// Eye space:
	private vector3					forw,      right,      down;
	private vector3					forw_unit, right_unit, down_unit;
	private vector3					forw_out,  right_out,  down_out;
	private vector3					view_pos;
	private double					hor_ang;
	private double					ver_ang;
	
	// Screen info:
	private double					tan_hor_fov_2;
	private double					tan_ver_fov_2;
	private double					width_tan_hor_fov_2;
	private double					height_tan_ver_fov_2;
	private double					pixel_stretch;
	private double					aspect_ratio;
	private double					screen_width;
	private double					screen_height;
	
	// Projection info:
	private vector3					diff;
	// private vector3					eye_pos;
	private double					depth;
	
	private double					deproj_depth;
	private vector2					deproj_normal;
	
	private vector3					angle_normal;
	
	// kd: You need to do these at least once.
	void CacheScreenInfo () {
		aspect_ratio	= Screen.GetAspectRatio();
		screen_width	= Screen.GetWidth();
		screen_height	= Screen.GetHeight();
		pixel_stretch	= level.pixelstretch;
	}
	
	void CacheFovInfo (double hor_fov = 90) {
		// kd: In gzd, any fov parameter is actually horizontal fov. Furthermore
		// this only holds true in 4/3. For any other aspect ratio, you simply
		// scale by your current aspect ratio over 4/3. Go figure, huh?
		tan_hor_fov_2	= tan(hor_fov / 2) * aspect_ratio / (4.0 / 3);
		tan_ver_fov_2	= tan_hor_fov_2 / aspect_ratio;
		height_tan_ver_fov_2	= screen_height	/ tan_ver_fov_2;
		width_tan_hor_fov_2		= screen_width	/ tan_hor_fov_2;
	}
	
	double TanHorFovHalf () const {
		return tan_hor_fov_2;
	}
	
	double TanVerFovHalf () const {
		return tan_ver_fov_2;
	}
	
	// kd: This must be done whenever the view changes.
	void Orient (vector3 view, double ang, double vang, double rang) {
		
		// Cache some view data for convenience later:
		hor_ang		= ang;
		ver_ang		= vang;
		view_pos	= view;
		
		// Begin projection stuff:
		vang = Gc_Ang.Stretch(pixel_stretch, vang);
		[forw_unit, right_unit, down_unit] =
			Gc_Vec.RolledViewOnb(ang, vang, rang);
	}
	
	ui void OrientForRenderOverlay (RenderEvent event) {
		Orient(
			event.viewpos,
			event.viewangle,
		-	event.viewpitch,
		-	event.viewroll);
	}
	
	void OrientForPlayer (PlayerInfo player) {
		let mo = player.mo;
		Orient(
			mo.vec3offset(0, 0, player.viewheight),
			mo.angle,
		-	mo.pitch,
		-	mo.roll);
	}
	
	// After that, you can call BeginProjection and BeginDeprojection to cache
	// these vectors for repeated use.
	void BeginProjection () {
		// kd: To do projections, we scale on the z-axis by the pixel stretch
		// factor, because world positions are scaled the same way.
		[forw, right, down] =
			Gc_Vec.ScaleZ(pixel_stretch, forw_unit, right_unit, down_unit);
	}
	
	void BeginDeprojection () {
		// kd: To do deprojections, we scale by the inverse, because we have to
		// compensate for the world being stretched.
		[forw_out, right_out, down_out] =
			Gc_Vec.ScaleZ(1 / pixel_stretch, forw_unit, right_unit, down_unit);
	}
	
	// kd: Now you can call "Begin" functions to cache a projection and find
	// out interesting stuff about them. This gives the most options most
	// cleanly.
	void ProjectWorldPos (vector3 pos) {
		diff	= levellocals.vec3diff(view_pos, pos);
		depth	= diff dot forw;
	}
	
	void ProjectDiff (vector3 my_diff) {
		diff	= my_diff;
		depth	= diff dot forw;
	}
	
	bool IsInFront () const {
		return 0 < depth;
	}
	
	// kd: If you don't see it: [forw, right, down] is the 3x3 rotation matrix
	// that rotates the world coordinate grid (the (1 0 0) (0 1 0) (0 0 1)
	// vectors) to your view, so x becomes forwards, y becomes right and
	// z becomes downwards.
	
	// If you're like me, you'll have an easier time just imagining it as a
	// batch of projections, though!
	
	// Anyway: This gives us normal (-1, 1) coordinates. Your screen is THIS:
	
	// (-1 -1)				(0, -1)				(1, -1)
	
	
	
	// (-1, 0)				(0, 0)				(1, 0)
	
	
	
	// (-1, 1)				(0, 1)				(1, 1)
	
	// It's just sixth grade triangle stuff packed up in kinda fun structures
	// with a bunch of gzd pixel stretch shenanigans. Honestly? Literally
	// copy paste this struct and Gc_Vec into your project. All your troubles
	// will be no more. Look for the gc_addon sandman visor.txt for an example.
	
	vector2 ToScreenNormal () const {
		return (
			diff dot right	/ tan_hor_fov_2,
			diff dot down	/ tan_ver_fov_2) / depth;
	}
	
	vector2 ToScreen () const {
		vector2 screen_pos = 0.5 * (ToScreenNormal() + (1, 1));
		return (screen_pos.x * screen_width, screen_pos.y * screen_height);
	}
	
	// kd: Or go in reverse.
	void DeprojectNormalDepth (vector2 normal, double depth) {
		deproj_normal = normal;
		deproj_depth = depth;
	}
	
	void DeprojectNormal (vector2 normal) {
		deproj_normal = normal;
	}
	
	void DeprojectDepth (double depth) {
		deproj_depth = depth;
	}
	
	vector3 ToDiff () const {
		return	deproj_depth * (
				tan_hor_fov_2 * deproj_normal.x * right_out +
				tan_ver_fov_2 * deproj_normal.y * down_out +
				forw_out);
	}
	
	vector3 ToWorld () const {
		return	view_pos + deproj_depth * (
				tan_hor_fov_2 * deproj_normal.x * right_out +
				tan_ver_fov_2 * deproj_normal.y * down_out +
				forw_out);
	}
	
	// kd: Or get angles.
	
	void DeprojectAngles (vector2 normal) {
		angle_normal = NormalToDiff(normal);
	}
	
	double ToWorldAngle () const {
		return Gc_Vec.Angle3(angle_normal);
	}
	
	double ToWorldPitch () const {
		return -Gc_Vec.Pitch(angle_normal);
	}
	
	double ToDiffAngle () const {
		return Gc_Vec.Angle3(angle_normal) - hor_ang;
	}
	
	double ToDiffPitch () const {
		return -(Gc_Vec.Pitch(angle_normal) + ver_ang);
	}
	
	// kd: Convenience stuff, for which you do not need to call the
	// Project* or Deproject* functions.
	
	vector3 NormalToWorld (vector2 normal, double forward_depth = 1) const {
		return	view_pos + forward_depth * (
				tan_hor_fov_2 * normal.x * right_out +
				tan_ver_fov_2 * normal.y * down_out +
				forw_out);
	}
	
	vector3 NormalToDiff (vector2 normal, double forward_depth = 1) const {
		return	forward_depth * (
				tan_hor_fov_2 * normal.x * right_out +
				tan_ver_fov_2 * normal.y * down_out +
				forw_out);
	}
}